<?php

namespace app\api\controller;

use addons\goods\Goods;
use app\common\controller\Api;
use app\common\model\level\Config;
use app\common\model\level\Log;
use app\common\model\MoneyLog;
use app\common\model\Redeem;
use app\common\model\shop\SkuGoods;
use app\common\model\User;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['index', 'vip_info', 'standing'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
   
        $time = time();
        // $friday = strtotime(date('Y-m-d 20:00:00', strtotime("Friday"))); //本周五开始时间
        $friday = strtotime('2023-12-13 10:06:00'); //本周五开始时间
        $lastfriday = strtotime(date('Y-m-d 20:00:00', strtotime("Friday"))); //下周五开始时间
        $sunday = strtotime(date('Y-m-d 20:00:00', strtotime("Sunday"))); //本周日开始时间
        $lastsunday = strtotime(date('Y-m-d 20:00:00', strtotime("Sunday"))); //下周日开始时间
        if ($time < $friday) {
            $endtime = $friday - $time;
        } elseif ($time > $sunday) {
            $endtime = $lastfriday - $time;
        } else {
            $endtime = '';
        }
        $banner = image_processing('images', config('site.banner'));
        $discount_goods = SkuGoods::where('type', '3')->where('status', '1')->order('sales desc,id desc')->select();
        $recommended_goods = SkuGoods::where('type', '2')->where('status', '1')->order('sales desc,id desc')->select();
        $this->success('请求成功', compact('banner', 'discount_goods', 'recommended_goods', 'endtime'));
    }
    /**
     * @Author LK
     * @ApiTitle (VIP详情)
     * @ApiMethod (get)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function vip_info()
    {
        $param = request()->param();
        extract($param);
        $levelInfo = Config::get(1);
        $this->success('', $levelInfo);
    }
    /**
     * @Author LK
     * @ApiTitle (身份升级)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function standing()
    {
        $list = Config::all(['id' => ['<>', 1]]);
        $this->success('', $list);
    }
    /**
     * @Author LK
     * @ApiTitle (开通会员)
     * @ApiMethod (POST)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function open_vip()
    {
        $data = Config::get(1);
        $data = [
            'orderid'   => sn('user_level', 'orderid', 'LV'),
            'user_id'   => $this->auth->id,
            'payamount' => $data->open_vip,
            'vip_id' => 1,
        ];
        Db::startTrans();
        try {
            $order = new Log($data);
            $order->allowField(true)->save();
            if ($order->id) {
                queue('app/common/job/LevelOrder', $order->id, 600, 'levelOrder1272');
            } else {
                exception('开通会员失败');
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error('支付失败：' . $e->getMessage());
        }
        $this->success('', $order->id);
    }

    /**
     * @Author LK
     * @date: 2023-11-23 
     * @ApiTitle (兑换兑换码)
     * @ApiMethod (GET)
     * @ApiMethod (POST)
     * @ApiParams (name="code", type="string", required=true, sample="", description="兑换码")
     * @ApiParams (name="address_id", type="string", required=true, sample="", description="地址")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function exchange()
    {
        $params = request()->param();
        extract($params);
        $info = Redeem::get(['code' => $code]);
        if (!$info) {
            $this->error('兑换码无效');
        }
        if ($info->status == '1') {
            $this->error('兑换码已兑换');
        }
        $info->address_id = $address_id;
        $info->user_id = $this->auth->id;
        $info->conversion_time = time();
        $info->status = '1';
        $res = $info->save();
        if ($res) {
            $this->success('兑换成功');
        }
        $this->error('兑换失败');
    }
}
